package processes.nMCounterProcess;

import os.MiniSemaphore;

/**
 * The count maintained by the {@link MCounterThread} threads. 
 */
public class Counter
{
	/**
	 * The main counter to watch
	 */
	private int counter;
	private MiniSemaphore accessCounter;

	/**
	 * Creates a counter and initiates it to 0
	 * @param accessCounter 
	 */
	public Counter(MiniSemaphore accessCounter)
	{
		counter = 0;
		this.accessCounter = accessCounter;
	}

	/**
	 * Increment the value of the counter
	 */
	public void increment()
	{
		accessCounter.P();
		counter++;
		accessCounter.V();
	}

	/**
	 * read the value of the counter
	 * @return the counter value
	 */
	public int read()
	{
		int result;
		accessCounter.P();
			result = counter;
			counter = 0;
		accessCounter.V();
		return result;
	}
}
